using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerAnimationController : MonoBehaviour
{
    private Animator anim;
    private Rigidbody2D rd;
    private PhysicsCheck phyiscsCheck;
    private PlayerInputController controllerInput;

    public float angle;

    private void Awake()
    {
        anim = GetComponent<Animator>();
        rd = GetComponent<Rigidbody2D>();
        phyiscsCheck = GetComponent<PhysicsCheck>();
        controllerInput = GetComponent<PlayerInputController>();
    }

    private void Update()
    {
       GetMouseAngle();
       SetAnimation();
    }

    private void SetAnimation()
    {
       anim.SetFloat("Angle",angle); 
       anim.SetBool("IsMove",controllerInput.isMove);
    }

    public void GetMouseAngle()
    {
        Vector2 mouseScreenPosition = Mouse.current.position.ReadValue();

        // 将屏幕坐标转换为世界坐标
        Vector3 mouseWorldPosition = Camera.main.ScreenToWorldPoint(new Vector3(mouseScreenPosition.x, mouseScreenPosition.y, 0));

        // 因为是2D游戏，z轴应该设置为0
        mouseWorldPosition.z = 0;
        // 计算鼠标位置与玩家位置的方向向量
        Vector3 direction = controllerInput.GetMousePosition() - transform.position;

        // 计算角度（弧度），并转换为度数
        float targetAngle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
        angle = targetAngle;
    }
}
